<?php

namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use backend\models\SysItem;
use backend\models\TaozhuangPos;

/**
 * This is the model class for table "grp_brand".
 *
 * @property integer $id
 * @property string $name
 * @property integer $order
 */
class GrpBrand extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'grp_brand';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order','factory_id','useing','head_grp'], 'integer'],
            [['name'], 'string', 'max' => 24],
            [['memo'], 'string', 'max' => 255],
            [['tax_rate'], 'number']

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => Yii::t('app','Brand Name'),
            'order' => Yii::t('app','Brand Order'),
            'factory_id'=>Yii::t('app','Factory Id'),
            'useing'=>Yii::t('app','Useing'),
            'head_grp'=>Yii::t('app','Head Grp'),
            'tax_rate'=>Yii::t('app', 'Tax Rate'),
        ];
    }
    public function getBrandFactory()
    {
    	return $this->hasOne(GrpFactory::className(),['id'=>'factory_id']);
    }
    public function getItems()
    {
        return $this->hasMany(SysItem::className(), ['brand_id'=>'id']);
    }
    public function getTaozhuangItems()
    {
        return $this->hasMany(TaozhuangPos::className(), ['item_id'=>'id'])->viaTable('sys_item',['brand_id'=>'id']);
    }
    public static function useingList()
    {
        return [1=>'使用',0=>'不使用'];
    }
    public static function allBrand()
    {
        $brand_array=ActiveRecord::findBySql("SELECT grp_brand.id,grp_brand.`name`,grp_brand.`order`,grp_brand_head.`name` AS brand_class,grp_brand_head.`order` AS brand_class_order
            FROM grp_brand LEFT JOIN grp_brand_head ON grp_brand.head_grp=grp_brand_head.id where grp_brand.useing=1 
            ORDER BY grp_brand_head.`order` DESC,grp_brand.`order` ASC;")->asArray()->all();
        $brand_array2=array();
        for ($i=0,$j=0;$i<count($brand_array);$i++)
        {
            if ($i==0||$i>0&&$brand_array[$i]["brand_class_order"]!=$brand_array[$i-1]["brand_class_order"]){
                $j=0;
            }
            $brand_array2[$brand_array[$i]["brand_class"]][$j++]=[
                'id'=>$brand_array[$i]['id'],
                'name'=>$brand_array[$i]['name']
            ];
        }
        return $brand_array2;
    }
    public function offTzposPaystateAndSysitemAppreturn()
    {
        $a=GrpBrand::findOne(['id'=>$this->id]);
        if (count($a->taozhuangItems))
        {
            $arr=[];$i=0;
            foreach ($a->taozhuangItems as $model)
            {
                $arr[$i++]=$model->id;
            }
            TaozhuangPos::updateAll(['pay_state'=>0],['id'=>$arr]);
        }
        SysItem::updateAll(['app_return'=>2],'brand_id='.$this->id);
    }
}
